/*
 * =====================================================================================
 *
 *       Filename:  arg.cpp
 *
 *    Description:  implementation of config file parsing module
 *
 *        Version:  1.0
 *        Created:  08/06/2009 11:55:18 AM
 *       Revision:  none
 *       Compiler:  gcc
 *
 *         Author:  Zhao Zhao 
 *        Company:  ndssl
 *
 * =====================================================================================
 */

#include "arg.h"
#include <fstream>
#include <iostream>
#include <string.h>

#include <cstdlib>
#include <stdio.h>
using namespace std;

Arg::Arg()
{}
Arg::~Arg()
{}

void Arg::Configure(const char* measure)
{
	string measureName = measure;
	cout<<"=================================================================="<<endl;
	cout<<"configure from file "<<measureName<<endl;
	ifstream ifp(measureName.c_str());
	if(!ifp.is_open())
	{
		cout<<"configure file open failed"<<endl;
		exit(1);
	}
	while(!ifp.eof())
	{
		char line[256];
		ifp.getline(line, 256);
		char* lineNew = ProcessComment(&line[0]);
		if(lineNew == NULL) continue;
		
		ParseParam(lineNew);
	}
	cout<<"configuration done"<<endl;
	cout<<"=================================================================="<<endl;
}
char* Arg::ProcessComment(char* line)
{
	while(line[0] == '\t' || line[0] == ' ')
	{
		line++;
	}
	if(line[0] == '#' || line[0] == '\n') return NULL;
	line = strtok(line, "#");
	return line;
}
void Arg::ParseParam(const char* line)
{
	cout<<"start to parse param"<<endl;
	char* cp = strdupa(line);
	char* att = strtok(cp," \t");
	string attstr = att;
	param[attstr].resize(0);
	cout<<"attr: "<<attstr<<"\t";
	cout<<"value: ";	
	char* str = strtok(NULL, " \t");
	while(str != NULL)
	{
		string value = str;
		param[attstr].push_back(value);
		cout<<value<<"\t";
		str = strtok(NULL, " \t");
	}
	if(param[attstr].empty())
	{
		cout<<"missing value"<<endl;
	}
	cout<<endl;
}
vector<string> Arg::GetValue(const char* attr)
{
	return param[attr];
}
